package cn.tedu.baking.controller;

import cn.tedu.baking.mapper.CommentMapper;
import cn.tedu.baking.mapper.ContentMapper;
import cn.tedu.baking.pojo.dto.CommentDTO;
import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.entity.Comment;
import cn.tedu.baking.pojo.entity.Content;
import cn.tedu.baking.pojo.ov.ContentIndexVO;
import cn.tedu.baking.pojo.ov.ContentManagementVO;
import cn.tedu.baking.pojo.ov.ContentUpdateVO;
import cn.tedu.baking.pojo.ov.UserVO;
import cn.tedu.baking.response.JsonResult;
import cn.tedu.baking.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/contents")
@Slf4j
public class ContentsController {

    @Autowired
    ContentMapper contentMapper;

    @Autowired
    CommentMapper commentMapper;

    @GetMapping("/{type}/{categoryId}/index")
    public JsonResult index(@PathVariable Integer type, @PathVariable Long categoryId) {
        System.out.println("type=" + type + "---categoryID=" + categoryId);
        List<ContentIndexVO> contentIndexVO = contentMapper.selectByTypeAndCategoryId(type, categoryId);
        return JsonResult.ok(contentIndexVO);
    }


//    @PostMapping("/add-new")
//    public JsonResult addNew(@RequestBody ContentDTO contentDTO) {
//        Content content = new Content();
//        BeanUtils.copyProperties(contentDTO, content);
//        content.setCreateTime(new Date());
//        contentMapper.insert(content);
//        return JsonResult.ok();
//    }

    @GetMapping("/{type}/management")
    public JsonResult management(@PathVariable Integer type, HttpSession
            session) {
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null) {
            //假设当前管理员登录
            user.setId(1L);
        }
        //查询文章的列表---查当前登录用户的
        //+查询条件 当前登录用户的id
        return
                JsonResult.ok(contentMapper.selectByType(type, user.getId()));
    }

    @GetMapping("/{id}/update")
    public JsonResult selectForUpdate(@PathVariable Long id) {
        return JsonResult.ok(contentMapper.selectByIdForUpdate(id));
    }

    //http://localhost:8080/v1/contents/add-new
    @PostMapping("/add-new")
    public JsonResult add(@RequestBody ContentDTO contentDTO,HttpSession
            session){
        log.info("文章新增对象:{}",contentDTO);
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
//1、做新增 做修改
        if (contentDTO.getId()!=null) {
            UserVO user = (UserVO)session.getAttribute("user");
            content.setUpdateBy(user.getId());
//获取登录用户的id
            content.setUpdateTime(new Date());
            contentMapper.update(content);
            return JsonResult.ok();
        }
//时间 创建时间
        content.setCreateTime(new Date());
//Content content
        contentMapper.insert(content);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/delete")
    public JsonResult delete(@PathVariable Long id) {
//得到封面图片路径并删除
        ContentUpdateVO contentUpdateVO = contentMapper.selectByIdForUpdate(id);
        new File("d:/file" + contentUpdateVO.getImgUrl()).delete();
        if (contentUpdateVO.getType() == 2) {//判断如果是视频则删除视频文件
            new File("d:/file" + contentUpdateVO.getVideoUrl()).delete();
        }
        contentMapper.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id}/detail")
    public JsonResult selectDetail(@PathVariable Long id) {
//让浏览量+1
        contentMapper.updateViewCountById(id);
        return JsonResult.ok(contentMapper.selectByIdForDetail(id));
    }

    @GetMapping("/{userId}/{contentId}/other")
    public JsonResult selectOther(@PathVariable Long userId,
                                  @PathVariable Long contentId) {
        return JsonResult.ok(contentMapper.selectByUserId(userId));
    }
    @GetMapping("/hot")
    public JsonResult selectHot() {
        return JsonResult.ok(contentMapper.selectHot());
    }

    @GetMapping("/{type}/list")
    public JsonResult selectList(@PathVariable Integer type) {
        return JsonResult.ok(contentMapper.selectListByType(type));
    }


    @GetMapping("/{wd}/search")
    public JsonResult search(@PathVariable String wd) {
        return JsonResult.ok(contentMapper.selectByWd(wd));
    }

    @GetMapping("{type}/admin")
    public JsonResult selectAdmin(@PathVariable Integer type) {
        return JsonResult.ok(contentMapper.selectByTypeForAdmin(type));
    }



}
